package com.zhuhjay.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhuhjay.commonutils.R;
import com.zhuhjay.servicebase.exception.GuLiException;
import com.zhuhjay.statistics.client.UcenterClient;
import com.zhuhjay.statistics.entity.StatisticsDaily;
import com.zhuhjay.statistics.mapper.StatisticsDailyMapper;
import com.zhuhjay.statistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author ZhuHJay
 * @since 2022-09-10
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Resource
    private UcenterClient ucenterClient;

    @Override
    @Transactional(rollbackFor = GuLiException.class)
    public void registerCount(String day) {
        R register = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) register.getData().get("countRegister");

        // 删除当天记录
        if (!this.remove(new LambdaQueryWrapper<StatisticsDaily>()
                .eq(StatisticsDaily::getDateCalculated, day))) {
            throw new GuLiException(20001, "统计失败");
        }

        StatisticsDaily statisticsDaily = new StatisticsDaily()
                .setDateCalculated(day)
                .setRegisterNum(countRegister)
                // TODO
                .setCourseNum(1)
                .setLoginNum(1)
                .setVideoViewNum(1);

        this.save(statisticsDaily);
    }

    @Override
    @SuppressWarnings("all")
    public Map<String, Object> showData(String type, String begin, String end) {
        Map<String, Object> map = new HashMap<>(2);
        // 获取区间数据
        List<StatisticsDaily> list = this.list(new LambdaQueryWrapper<StatisticsDaily>()
                        .select(StatisticsDaily.class,
                                filed -> "date_calculated".equals(filed.getColumn()) || type.equals(filed.getColumn()))
                        .between(StatisticsDaily::getDateCalculated, begin, end)
                        .orderByAsc(StatisticsDaily::getDateCalculated));

        List<Integer> numDataList = new ArrayList<>();
        List<String> calculatedList = new ArrayList<>();
        for (StatisticsDaily statisticsDaily : list) {
            numDataList.add(getTypeValue(statisticsDaily, type));
            calculatedList.add(statisticsDaily.getDateCalculated());
        }

        map.put("numDataList", numDataList);
        map.put("date_calculatedList", calculatedList);
        return map;
    }

    private Integer getTypeValue(StatisticsDaily statisticsDaily, String type) {
        switch (type) {
            case "register_num": return statisticsDaily.getRegisterNum();
            case "login_num": return statisticsDaily.getLoginNum();
            case "video_view_num": return statisticsDaily.getVideoViewNum();
            case "course_num": return statisticsDaily.getCourseNum();
            default: return 0;
        }
    }


}
